<!--
 * @Description: 
 * @Author: charles
 * @Date: 2021-11-11 11:19:29
 * @LastEditors: charles
 * @LastEditTime: 2021-11-11 12:07:21
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>异常边界</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/react/17.0.2/umd/react.development.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.2/umd/react-dom.development.min.js"></script>
  <script src="https://cdn.bootcss.com/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
  <div id="app"></div>
  <script type="text/babel">
    // 异常边界组件 用友 = 招聘
    class ErrorBoundary extends React.Component {
      constructor(props){
        super(props);
        this.state = {
          hasError:false
        }
      }
      static getDerivedStateFromError(){
        return {
          hasError:true
        };
      }
      componentDidCatch(error){
        alert('ajax上报'+error.message+error.stack);
      }

      render(){
        if(this.state.hasError){
          return <div>对不起！页面异常，请联系管理员licy@briup.com</div>
        } else {
          return this.props.children;
        }
      }
    }

    class List extends React.Component{
      render(){
        return (
          <div>
            <h3>列表</h3>  
            <div>num: {this.props.num}</div>
          </div>
        )
      }
    }
    class App extends React.Component {
      render(){
        return (
          <div>
            <h2>组件通信</h2>  
            {
              Math.random() > 0.5? <List num={1}></List>:<Info/>
            }
          </div>
        )
      }
    }
    ReactDOM.render(
      <ErrorBoundary>
        <App/>
      </ErrorBoundary>,
      document.getElementById('app'));
  </script>
</body>
</html>